package com.zhiyou100.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.annotation.Auth;
import com.zhiyou100.model.Course;
import com.zhiyou100.model.Subject;
import com.zhiyou100.model.Video;
import com.zhiyou100.serice.CourseService;
import com.zhiyou100.serice.SubjectService;
import com.zhiyou100.serice.VideoSerice;
import com.zhiyou100.util.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

@Controller
public class CourseController {
    @Autowired
    private CourseService service;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private VideoSerice videoSerice;
    @Autowired
    private JedisPool jedis;


    @RequestMapping(value = "/course", method = RequestMethod.GET)
    @ResponseBody @Auth("这是一个查询Course全部的操作")
    public ResultObject findAll(Integer pageNo) {
        if (pageNo == null) {
            pageNo = 1;
        }
        PageHelper.startPage(pageNo, 3);
        List<Course> courses = service.findAll();
        PageInfo<Course> pageInfo = new PageInfo<Course>(courses);
        ArrayList<Object> lists = new ArrayList<Object>();
        lists.add(courses);
        lists.add(pageInfo);
        return new ResultObject("200", "查询全部speaker", lists);
    }

    @RequestMapping(value = "/course/pageInfo/{id}", method = RequestMethod.GET)
    @ResponseBody @Auth("这是一个分页查询Course全部的操作")
    public ResultObject findAllPageInfo(@PathVariable("id") Integer id) {
        return findAll(id);
    }

    @RequestMapping(value = "/course/{id}", method = RequestMethod.GET)
    @ResponseBody @Auth("这是一个通过id查询一个Course的操作")
    public ResultObject findById(@PathVariable("id") Integer id) {
        ArrayList<Object> lists = new ArrayList<Object>();
        Course course = service.findById(id);
        List<Subject> subjects = subjectService.findAll();
        lists.add(course);
        lists.add(subjects);
        return new ResultObject("200", "查询一个speaker", lists);
    }

    @RequestMapping(value = "/course/{id}", method = RequestMethod.PUT)
    @ResponseBody @Auth("这是一个通过id更新一个Course的操作")
    public ResultObject update(@PathVariable("id") Integer id, @RequestBody Course course) {
        course.setId(id);
        System.out.println(course);
        service.update(course);
        Jedis resource = jedis.getResource();
        resource.del("*");
        return new ResultObject();
    }

    @RequestMapping(value = "/course", method = RequestMethod.POST)
    @ResponseBody @Auth("这是一个添加一个Course的操作")
    public ResultObject add(@RequestBody Course course) {
        service.add(course);
        Jedis resource = jedis.getResource();
        resource.del("*");
        return new ResultObject();
    }

    @RequestMapping(value = "/course/{id}", method = RequestMethod.DELETE)
    @ResponseBody @Auth("这是一个删除一个Course的操作")
    public ResultObject delete(@PathVariable("id") Integer id) {
        service.delete(id);
        Jedis resource = jedis.getResource();
        resource.del("*");
        return new ResultObject("200", "删除一个course", "success");
    }

    @RequestMapping(value = "/subjectAll", method = RequestMethod.GET)
    @ResponseBody @Auth("这是一个通过subjectId查询Course的操作")
    public ResultObject findAllSubject() {
        List<Subject> lists = subjectService.findAll();
        return new ResultObject("200", "查询全部Subject", lists);
    }

    @RequestMapping(value = "/course/findBySubject/{id}", method = RequestMethod.GET)
    @ResponseBody @Auth("这是一个通过subjectId查询Course的前台操作")
    public ResultObject findBySubject(@PathVariable("id") Integer id) {
        ArrayList lists = new ArrayList();
        List<Video> videos = new ArrayList<Video>();
        List<Course> courses;
        Jedis resource = jedis.getResource();
        System.out.println(resource.keys("*"));
        try {
            Long strlen = resource.strlen("findBySubject:" + id);
            System.out.println("id是 : " + id);
            System.out.println(strlen);
            System.out.println(resource.get("findBySubject:" + id));
            if (strlen != 0) {
                String s = resource.get("findBySubject:" + id);
                List arrayLists = JSONArray.parseArray(s);
                for (int i = 0; i <= arrayLists.size(); i++) {
                    if (i == 0) {
                        courses = JSONArray.parseArray( arrayLists.get(i).toString(), Course.class);
                        lists.add(courses);
                    } else if (i == 1) {
                        videos = JSONArray.parseArray( arrayLists.get(i).toString(), Video.class);
                        lists.add(videos);
                    } else if (i == 2) {
                        Subject subject = JSONObject.parseObject( arrayLists.get(i).toString(), Subject.class);
                        lists.add(subject);
                    }
                }
                System.out.println("-------------------redis执行了---------------------");
            } else {
                courses = service.findBySubjectId(id);
                for (Course c : courses) {
                    List<Video> videos1 = videoSerice.findByCourseId(c.getId());
                    for (Video v : videos1) {
                        videos.add(v);
                    }
                }
                Subject subject = subjectService.findById(id);
                lists.add(courses);
                lists.add(videos);
                lists.add(subject);
                String json = JSONArray.toJSON(lists).toString();
                resource.set("findBySubject:" + id, json);
                System.out.println(resource.get("findBySubject:" + id));
                System.out.println(resource.keys("*"));
                System.out.println(resource.strlen("findBySubject:" + id));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resource.close();
        return new ResultObject("200", "查询成功", lists);
    }

}
